package com.yfqy.admin.util;

import com.yfqy.admin.domain.dto.auth.LoginUserInfoDTO;
import com.yfqy.admin.security.login.LoginUserInfo;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: 梁二东
 * @Date: 2025/5/9 13:08
 * @Description:
 **/
public class LoginUserInfoTool {

    // 手动转换
    public static LoginUserInfo convert(LoginUserInfoDTO dto) {
        LoginUserInfo info = new LoginUserInfo();
        info.setSessionId(dto.getSessionId());
        info.setId(Long.parseLong(dto.getId()));
        info.setExpiredTime(Long.parseLong(dto.getExpiredTime()));
        info.setNickname(dto.getNickname());
        info.setLoginType(dto.getLoginType());

        info.setAuthorities(
                Optional.ofNullable(dto.getAuthorities())
                        .orElseGet(Collections::emptyList)
                        .stream()
                        .map(map -> new SimpleGrantedAuthority(map.get("authority")))
                        .collect(Collectors.toList())
        );
        return info;
    }

}
